** This dofile makes the results related to new and old partner rank correlation

/*
Figure 4: Old and New Partner Ranks 

Figure A5: Old and New Partner Ranks (Binding)

Table A24: B: Old and New Partners 2004 E7: OLS
*/

* Calculate number of importers by hs6 in 2004
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict", clear
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
collapse (max) value_exporter_importer, by(newclid2 hs6)
tostring newclid2, generate(importer_id)
gen hs6_importer_id=hs6+"_"+ importer_id
gsort hs6 -value_exporter_importer newclid2
by hs6: gen importer_own_rank2=_n
by hs6: gen n_importer_2004_3=_N
keep hs6_importer_id importer_own_rank2 n_importer_2004_3 hs6
merge 1:1 hs6_importer_id using "${output_data}importer_rank.dta"
collapse n_importer_2004, by(hs6)
save  "${output_data}n_importer.dta", replace

* Calculate number of exporters by hs6 in 2004
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict", clear
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
collapse (max) value_exporter_importer, by(rfc hs6)
gen hs6_exporter_id=hs6+"_"+ rfc
gsort hs6 -value_exporter_importer rfc
by hs6: gen exporter_own_rank2=_n
by hs6: gen n_exporter_2004_3=_N
keep hs6_exporter_id exporter_own_rank2 n_exporter_2004_3 hs6
merge 1:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
collapse n_exporter_2004, by(hs6)
save  "${output_data}n_exporter.dta", replace


** For Figure 4 and A5
** US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", replace
drop if hs6_importer_id==""
merge 1:1 hs6_importer_id using "${output_data}importer_rank.dta"
drop _merge
merge m:1 hs6 using "${output_data}n_exporter.dta"
tostring hs6, replace
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
gen rychange=e_mv_rank_d0704/n_exporter_2004
gen scaled_mv_rank2004=(e_mv_rank2004-1)/(n_exporter_2004-1)
gen scaled_mv_rank2007=(e_mv_rank2007-1)/(n_exporter_2004-1)
keep if rychange!=0
keep if scaled_mv_rank2007!=. & scaled_mv_rank2004!=.

#delimit;
twoway  
(scatter scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0)
(lfit scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0, lwidth(thick) lcolor(black)),
scheme(s1mono) legend(off)
 xtitle("Old partner's normalized rank (X)", size(large))  ytitle("New partner's normalized rank (Y)", size(large))
  title("US importers");
 graph export "${output}new_old_rank_correlation/new_old_rank_correlation_US.png", as(png) replace;
#delimit cr

#delimit;
twoway  
(scatter scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0 & Binding==1)
(lfit scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0 & Binding==1, lwidth(thick) lcolor(black)),
scheme(s1mono) legend(off)
 xtitle("Old partner's normalized rank (X)", size(large))  ytitle("New partner's normalized rank (Y)", size(large))
  title("US importers (Binding=1)");
 graph export "${output}new_old_rank_correlation/new_old_rank_correlation_US_binding.png", as(png) replace;
#delimit cr

* Fitted line
eststo clear
reg scaled_mv_rank2007 scaled_mv_rank2004, cluster(hs6)
eststo US
reg scaled_mv_rank2007 scaled_mv_rank2004 if Binding==1, cluster(hs6)
eststo US_binding

 
** Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", replace
drop if hs6_exporter_id==""
merge 1:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
drop _merge
merge m:1 hs6 using "${output_data}n_importer.dta"
rename iq_mex_bind80 Binding
gen rychange=mv_rank_d0704/n_importer_2004
gen scaled_mv_rank2004=(mv_rank2004-1)/(n_importer_2004-1)
gen scaled_mv_rank2007=(mv_rank2007-1)/(n_importer_2004-1)
keep if rychange!=0
keep if scaled_mv_rank2007!=. & scaled_mv_rank2004!=.

#delimit;
twoway  
(scatter scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0)
(lfit scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0, lwidth(thick) lcolor(black)),
scheme(s1mono) legend(off)
 xtitle("Old partner's normalized rank (X)", size(large))  ytitle("New partner's normalized rank (Y)", size(large))
  title("Mexican exporters") ;
 graph export "${output}new_old_rank_correlation/new_old_rank_correlation_mexico.png", as(png) replace;
#delimit cr

#delimit;
twoway  
(scatter scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0 & Binding==1)
(lfit scaled_mv_rank2007 scaled_mv_rank2004 if rychange!=0 & Binding==1, lwidth(thick) lcolor(black)),
scheme(s1mono) legend(off)
 xtitle("Old partner's normalized rank (X)", size(large))  ytitle("New partner's normalized rank (Y)", size(large))
  title("Mexican exporters (Binding=1)") ;
 graph export "${output}new_old_rank_correlation/new_old_rank_correlation_mexico_binding.png", as(png) replace;
#delimit cr

* Fitted line
reg scaled_mv_rank2007 scaled_mv_rank2004, cluster(hs6)
eststo Mexico
reg scaled_mv_rank2007 scaled_mv_rank2004 if Binding==1, cluster(hs6)
eststo Mexico_binding

# delimit ;
esttab US US_binding Mexico Mexico_binding using "${output}new_old_rank_correlation/new_old_rank_correlation.tex", replace nonumbers constant label
 starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(2)) se(par fmt(2))) stats(r2 N, fmt(%9.3f %9.0f) labels(R-Squared Obs.))
 posthead( \multicolumn{4}{c}{New rank} \\ US & US(Bind) & Mexico & Mexico(Bind)\\ \hline)
 nomtitles coeflabels(scaled_mv_rank2004 "Old rank" _cons "constant") collabels(,none) substitute((d));
#delimit cr



** For Table A24, B. Old and New Partners 2004 E7: OLS
eststo clear
* US
* total
use "${output_data}US_for_analysis_04_07_hs6_restrict_total.dta", clear
merge m:1 hs6 using "${output_data}n_exporter.dta"
gen rychange=e_mv_rank_d0704/n_exporter_2004
assert rychange>=-1 & rychange<=1 if rychange!=.
gen scaled_rank2004=(e_mv_rank2004-1)/(n_exporter_2004-1)
gen scaled_rank2007=(e_mv_rank2007-1)/(n_exporter_2004-1)
label var scaled_rank2004 "Old Partner Rank"
eststo:reg scaled_rank2007 scaled_rank2004 if rychange!=0, cluster(hs6)

* price
use "${output_data}US_for_analysis_04_07_hs6_price_restrict.dta", clear
drop if hs6_cve_importer_id==""
gen rychange=e_p_rank_d0704/n_exporters_2004
gen scaled_rank2004=(p_rank2004-1)/(n_exporters_2004-1)
gen scaled_rank2007=(p_rank2007-1)/(n_exporters_2004-1)
label var scaled_rank2004 "Old Partner Rank"
eststo:reg scaled_rank2007 scaled_rank2004 if rychange!=0 , cluster(hs6)

* quality
use "${output_data}/US_for_analysis_04_07_hs6_quality_restrict.dta", clear
gen rychange=e_new_rank_d0704/n_exporters_2004
assert rychange>=-1 & rychange<=1 if rychange!=.
gen scaled_rank2004=(new_rank2004-1)/(n_exporters_2004-1)
gen scaled_rank2007=(new_rank2007-1)/(n_exporters_2004-1)
label var scaled_rank2004 "Old Partner Rank"
eststo:reg scaled_rank2007 scaled_rank2004 if rychange!=0, cluster(hs6)


* Mexico
* total
use "${output_data}mexico_for_analysis_04_07_hs6_restrict_total.dta", clear
merge m:1 hs6 using "${output_data}n_importer.dta"
gen rychange=mv_rank_d0704/n_importer_2004
assert rychange>=-1 & rychange<=1 if rychange!=.
gen scaled_rank2004=(mv_rank2004-1)/(n_importer_2004-1)
gen scaled_rank2007=(mv_rank2007-1)/(n_importer_2004-1)
label var scaled_rank2004 "Old Partner Rank"
eststo: reg scaled_rank2007 scaled_rank2004 if rychange!=0, cluster (hs6)

* price
use "${output_data}mexico_for_analysis_04_07_hs6_price_restrict.dta", clear
rename iq_mex_bind80 Binding 
gen rychange=p_rank_d0704/n_importers_2004
gen scaled_rank2004=(p_rank2004-1)/(n_importers_2004-1)
gen scaled_rank2007=(p_rank2007-1)/(n_importers_2004-1)
label var scaled_rank2004 "Old Partner Rank"
eststo:reg scaled_rank2007 scaled_rank2004 if rychange!=0, cluster (hs6)

* quality
use "${output_data}mexico_for_analysis_04_07_hs6_quality_restrict.dta", clear
gen rychange=new_rank_d0704/n_importers_2004
assert rychange>=-1 & rychange<=1 if rychange!=.
gen scaled_rank2004=(new_rank2004-1)/(n_importers_2004-1)
gen scaled_rank2007_2=(new_rank2007-1)/(n_importers_2004-1)
label var scaled_rank2004 "Old Partner Rank"
eststo:reg scaled_rank2007 scaled_rank2004 if rychange!=0, cluster (hs6)

# delimit ;
esttab using "${output}new_old_rank_correlation/rank_correlation_alternative_rank_measures.tex", replace nonumbers constant label
 starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(2)) se(par fmt(2))) stats(r2 N, fmt(%9.3f %9.0f) labels(R-Squared Obs.))
 posthead( \multicolumn{6}{c}{New rank} \\ & \multicolumn{3}{l}{US}&\multicolumn{3}{l}{Mexico}\\ & Total & Price & Quality & Total & Price & Quality \\ \hline)
 nomtitles coeflabels(_cons "constant") collabels(,none) substitute((d));
# delimit cr



